Generating subtotal reports direct from SQL - Mailing list pgsql-general

From Owen Hartnett
Subject Generating subtotal reports direct from SQL
Date
Msg-id p06240803c331a68f30d9@[192.168.0.109]
Whole thread Raw
In response to Re: slow request  (Steve Atkins <steve@blighty.com>)
Responses Re: Generating subtotal reports direct from SQL  (Erik Jones <erik@myemma.com>)
Re: Generating subtotal reports direct from SQL  (Gregory Stark <stark@enterprisedb.com>)
List pgsql-general
I'm hoping there's a real easy way of doing this that I'm just missing:

Given a Select statement such as:

Select ID, code, amount from foo where code < 10;

that gives me a table like this:

ID    code        amount
_____________________________________
1    4        20
2    3        10
3    4        15
4    2        10
5    3        9
6    3        8

I want to generate a report table like the following (group by code):

ID    code        amount
_____________________________________
4    2        10
    2        10
2    3        10
5    3        9
6    3        8
    3        27
1    4        20
3    4        15
    4        35
            72

Such that the final table has additional subtotal rows with the
aggregate sum of the amounts.  I'm thinking I can generate two tables
and merge them, but is there an easier way using a fancy Select
statement?

-Owen

pgsql-general by date:

Previous
From: Steve Atkins
Date:
Subject: Re: slow request
Next
From: Erik Jones
Date:
Subject: Re: Generating subtotal reports direct from SQL